/*
 * (C) 2007-2010 Taobao Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *
 *
 * Version: 0.1: cloudbase.cpp,v 0.1 2011/08/27 09:15:48 chuanhui Exp $
 *
 * Authors:
 *   chuanhui <rizhao.ych@taobao.com>
 *     - some work details if you want
 *
 */
#include "cloudbase.h"

namespace cloudbase {
  CloudBase::CloudBase() {
    dbInstance_ = NULL;
  }

  CloudBase::~CloudBase() {
    if (NULL != dbInstance_)
    {
      // close DB
      delete dbInstance_;
      dbInstance_ = NULL;
    }
  }

  CloudBase* CloudBase::getInstance() {
    static CloudBase* instance = NULL;
    if (NULL == instance)
    {
      instance = new CloudBase();
    }
    return instance;
  }

  int CloudBase::init(const std::string& dbPath) {
    path_ = dbPath;
    return 0;
  }

  leveldb::DB* CloudBase::getDBInstance() {
    if (NULL == dbInstance_) {
      leveldb::Options options;
      options.create_if_missing = true;

      leveldb::Status status = leveldb::DB::Open(options, path_, &dbInstance_);
      assert(status.ok() && NULL != dbInstance_);
    }

    return dbInstance_;
  }
}
